//
//  WTCommunityViewController.m
//  OneDay
//
//  Created by lanou on 16/1/15.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTCommunityViewController.h"
#import "WTSquareViewController.h"
#import "WTSquareDetailViewController.h"

@interface WTCommunityViewController ()<UIScrollViewDelegate>

// segment
@property (nonatomic, strong) UISegmentedControl * segment;

// 底部scrollView
@property (nonatomic, strong) UIScrollView * scrollView;
// 广场
@property (nonatomic, strong) WTSquareViewController * squareVC;
// 最热
@property (nonatomic, strong) WTSquareViewController * hotVC;

// 存放广场,最热,圈子的数组
@property (nonatomic, strong) NSArray * nameArray;

@end

@implementation WTCommunityViewController

- (void)viewDidAppear:(BOOL)animated
{
    if (self.tabBarController.tabBar.hidden == YES) {
        self.tabBarController.tabBar.hidden = NO;
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"社区";
    
    
    _nameArray = @[@"广场",@"最热",@"圈子"];
    
    _segment = [[UISegmentedControl alloc]initWithItems:_nameArray];
    _segment.frame= CGRectMake(0, 64, kScreenWidth, 30 * kRelativelength_Y);
    _segment.backgroundColor = [UIColor whiteColor];
    _segment.selectedSegmentIndex = 0;
    [_segment addTarget:self action:@selector(selectedSegmentChanged:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:_segment];
    
    // 给导航栏添加返回按钮
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"m-fanhui"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftButtonClick:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
    [self addScrollView];
    // Do any additional setup after loading the view.
}
#pragma mark -- 返回按钮
- (void)leftButtonClick:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -- 添加底部的scrollView
- (void)addScrollView
{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _segment.bottom, kScreenWidth, kScreenHeight - _segment.bottom - 49)];
    _scrollView.contentSize = CGSizeMake(kScreenWidth * 3, _scrollView.height);
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = YES;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.alwaysBounceHorizontal = YES;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    
    // 添加广场
    _squareVC = [[WTSquareViewController alloc]init];
    _squareVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - _segment.bottom - 49);
    [_scrollView addSubview:_squareVC.view];
    [_squareVC requestData:@"square"];
   
    // block方法,实现向详情跳转的方法
    __block WTCommunityViewController * mySelf = self;
    _squareVC.block = ^(NSString *tid, NSString *fid){
        
        WTSquareDetailViewController * detailVC = [[WTSquareDetailViewController alloc]init];
        detailVC.tid = tid;
        detailVC.fid = fid;
     
        [mySelf.navigationController pushViewController:detailVC animated:YES];
    };

    // 添加最热
    _hotVC = [[WTSquareViewController alloc]init];
    _hotVC.view.frame = CGRectMake(kScreenWidth, 0, _squareVC.view.width, _squareVC.view.height);
    [_scrollView addSubview:_hotVC.view];
    _hotVC.block = ^(NSString *tid, NSString *fid){
        
        WTSquareDetailViewController * detailVC = [[WTSquareDetailViewController alloc]init];
        detailVC.tid = tid;
        detailVC.fid = fid;
        
        [mySelf.navigationController pushViewController:detailVC animated:YES];
    };
}

#pragma mark -- segment值改变的方法
- (void)selectedSegmentChanged:(UISegmentedControl *)segment
{
    NSInteger index = segment.selectedSegmentIndex;
    [_scrollView setContentOffset:CGPointMake(index * kScreenWidth, 0) animated:YES];
    if (segment.selectedSegmentIndex == 0) {
        [_squareVC requestData:@"square"];
    }else if (segment.selectedSegmentIndex == 1){
        [_hotVC requestData:@"hot"];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / kScreenWidth;
    _segment.selectedSegmentIndex = index;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
